//index.js
//获取应用实例
var valid1=require('../../../utils/valid')
var app = getApp()
Page({
  data: {
    imgurl:app.urlimg,
    kd_no:false,
    data:[],
   submitNum:0,
    order_goods_id:'',
     data2: {
    height: 20,
    focus: false,
    type:'退款' },
      index: 0,
      gindex:0,//收货状态
      rindex:0,
      phone:'',
      evaContent:'',//退货说明
     tjtype:['我要退款(无需退货)', '我要退款退货'],
   goodsStatus:['未收到货','已收到货'],
   refundDesc:['不喜欢/不想要','拍错了/订单信息错误','未按约定时间发货','其他原因'],//默认未发货
		img_box:[],
    reason:'',//原因
    refunds_id:0,
    loadHidden:false,
		text_length:50,

  },
  onLoad:function(e){
    var that = this;
    var refunds_id=typeof(e.refunds_id) == 'undefined' ?0:e.refunds_id
    wx.request({
												url: app.url + 'api_refunds/refunds_edit',
                        data: {
                          order_id:e.order_id,
                          order_goods_id:e.order_goods_id,
                          refunds_id:refunds_id,
                        },
                        header: {
                          'content-type': 'application/json',
                          'token':wx.getStorageSync('token_key')
                        }, success: function (json) {
                          console.log(json);
                          /*if(typeof(e.type)!='undefined'){
                            that.data.type=e.type
                               wx.setNavigationBarTitle({
                                title:'申请'+ e.type,
                              })
                            }*/
                            if(refunds_id!==0){
                            that.data.type=e.type
                               wx.setNavigationBarTitle({
                                title:'修改申请',
                              })
                            }
                              var data= json.data.result
                              var order_goods_id='';
															var goods_price=0;
															//console.log(data.goods);return
                             for(var i=0;i<data.goods.length;i++){
                              var dh=','
															goods_price += Number(data.goods[i].goods_price);
                              if(i==(data.goods.length-1)){
                                dh=""
                              }
                              order_goods_id+=data.goods[i].id+dh
                             }
														 goods_price.toFixed(2)
                            // var refundDesc=[]
                             var tjtype=[]
                             if(json.data.result['distribution_status']==0){
                               tjtype=['我要退款(无需退货)']
                              }else{
                             tjtype=['我要退款(无需退货)', '我要退款退货']
                              }
                              
                            that.setData({
                              tjtype:tjtype,
                              order_goods_id:e.order_goods_id,
                            data:json.data.result,
                            type: that.data.type,
														goods_price: goods_price,
                            index:refunds_id==0?0:json.data.result['refund_type'],
                            gindex:refunds_id==0?0:json.data.result['delivery_status'],
                            evaContent:json.data.result['refund_desc'],
                            reason:json.data.result['reason'],
                            phone:json.data.result['phone'],
                            img_box:refunds_id==0?[]:json.data.result['img'],
                            refunds_id:refunds_id,
                            //refundDesc:refunds_id==0?that.data.refundDesc:refundDesc,
                            //rindex:refunds_id==0?0:json.data.result['reason'],
                              loadHidden:true,
                          });
                            console.log( that.data)
                                 console.log( 666)
                        }
                      })
  },tjtypeChange:function(e){
 this.setData({
        index: e.detail.value
      });
  },
  goodsStatus:function(e){
  var kd_no=false  
var refundDesc=[]
    if(e.detail.value==0){
      refundDesc=['不想买了','拍错了/订单信息错误','未收到货','收到商品破损','七天无理由','其他原因']
    }else{
      refundDesc=['质量问题',  '卖家发错货', '货物与描述不符', '收到商品破损','七天无理由','其他原因']
     // kd_no=true  //显示快单号框
    }

 this.setData({
        gindex: e.detail.value,
        refundDesc:refundDesc,
        kd_no:kd_no,
      });

  },refundDesc:function(e){
    this.setData({
        rindex: e.detail.value
      });


  },
	//判断字段
	inputting: function (e) {
		var text_length=''
		if (e.detail.value.length<51){
			text_length = 50 - e.detail.value.length;
		}
		this.setData({
			text_length: text_length,
		})
	},
	//添加图片
	add_img:function(){
		var that = this;
		var img_box = that.data.img_box;
    console.log(img_box)
		if (img_box.length<3||that.data.redfunds_id==0){
			wx.chooseImage({
				count: 3, // 默认9  
				sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有  
				sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
				success: function (res) {
					// 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片 
					console.log(res);
					   var tempFilePaths = res.tempFilePaths
    wx.uploadFile({
            url: app.url + 'api_refunds/upload_img',
             header: {
                      'content-type': 'application/json',
                      'token':wx.getStorageSync('token_key')
                    },
             filePath: tempFilePaths[0],
             name: 'file',
            formData: {
            },
           success: function (json) {
            console.log(json);
           var data=  JSON.parse(json.data);
            if(data.done==true){
            img_box.push(data.result.img);
             that.setData({
            img_box: img_box,
          })



             /*var add = _this.data.add_img;
              add.push(tempFilePaths);
             var re_img = _this.data.re_img;
             re_img.push(data.result.img);
              _this.setData({
                add_img: add,
                re_img: re_img,
              })*/



            }else{
                 wx.showModal({
         title: '提示',
         content: '系统繁忙，请稍后再试',
         success: function (res) {
           if (res.confirm) {
           
           }
         }
       })
            }
             
 
            }
           })

					console.log(that.data.img_box);
				}
			})
		}else{
			wx.showToast({
				title: '图片已达上限',  //标题  
				icon: 'loading',  //图标，支持"success"、"loading"   
				duration: 1200, //提示的延迟时间，单位毫秒，默认：1500  
			}) 
		}
	},
	dele_img: function (e) {
		var index = e.currentTarget.dataset.index;
		var dele = e.currentTarget.dataset.index;
		var that=this;
		var img_box = that.data.img_box;
		if(index==0)
		dele=1+index;
		img_box.splice(index, dele);
		that.setData({
			img_box: img_box,
		})
	},
bindCheckbox:function(e){
  var that=this
  var order_goods_id=''
 var dh=','
  for(var i=0;i<that.data.data.goods.length;i++){
   // console.log(e.currentTarget.dataset.value+'---'+that.data.data.goods[i].id)
    if(e.currentTarget.dataset.value==that.data.data.goods[i].id){
       that.data.data.goods[i].type=that.data.data.goods[i].type=='circle'?'success_circle':'circle'
    }
       if(that.data.data.goods[i].type=='success_circle'){
       order_goods_id+=that.data.data.goods[i].id+dh
        }
  }
   order_goods_id=order_goods_id.substr(0,order_goods_id.length-1)
 // console.log(that.data.data)
  that.setData({'data':that.data.data,
    order_goods_id:order_goods_id
  })
 // console.log( order_goods_id);

},
  goodslink:function(e){
    wx.redirectTo({
      url:'/pages/index/detail/index?id='+e.currentTarget.id
    })
  },
    bindTextAreaBlur: function(e) {
   //console.log(e.detail.value)
  },
  bindFormSubmit: function(e) {
		var that = this
  var data= that.data.data
var order_id=that.data.data.id
var order_goods_id=that.data.order_goods_id
     if(that.data.index==1){
      if (that.data.gindex == '') {
      wx.showModal({
        title: '提示',
        content: '选择货物状态',
        success: function (res) {
          if (res.confirm) {

          }
        }
      })
      return false
      } 
     }
		

		if (that.data.rindex == '') {
			wx.showModal({
				title: '提示',
				content: '选择退款原因',
				success: function (res) {
					if (res.confirm) {

					}
				}
			})
			return false
		}

if (e.detail.value.evaContent == '') {
      wx.showModal({
        title: '提示',
        content: '请填退款说明',
        success: function (res) {
          if (res.confirm) {

          }
        }
      })
      return false
    }

       /*if (valid1.mobile(e.detail.value.phone)==false||e.detail.value.phone.length<7){
       wx.showModal({
         title: '提示',
         content: '手机号码格式不正确',
         success: function (res) {
           if (res.confirm) {
          
           }
         }
       })
         return
     }*/


     var opacity='###处理方式:'+that.data.tjtype[that.data.index]+'###'+
     '货物状态:'+that.data.goodsStatus[that.data.gindex]+'###'+
'退款原因:'+that.data.refundDesc[that.data.rindex]+'###'+
'手机号:'+e.detail.value.phone+'###'+
/*'快递单号:'+e.detail.value.kd_no+'###'+*/
//'备注信息:' + e.detail.value.txtDesc + '###'
			 '备注信息:' + e.detail.value.evaContent + '###' 
		
		 console.log(opacity);
     var img_box= that.data.img_box
      var re_imgs='';
      console.log(img_box)
         console.log(8888)
      if( img_box!==[]){
        for(var i=0;i<img_box.length;i++){
        re_imgs+=img_box[i]+',';
      }
      }

        //防止卡顿提交多次
        that.data.submitNum=that.data.submitNum+1;
        that.setData({'submitNum':that.data.submitNum})
      console.log(that.data.submitNum)
      if(that.data.submitNum==1){
         wx.request({
           url: app.url + 'api_refunds/refund',
                        data: {
                          order_goods_id:order_goods_id,
                          order_id:order_id,
                          content:opacity,
                          formid:e.detail.formId,
                          refunds_id:that.data.refunds_id,
                          refund_type:that.data.index,
                          delivery_status:that.data.gindex,
                          reason:that.data.refundDesc[that.data.rindex],
                          phone:e.detail.value.phone,
                          refund_desc:e.detail.value.evaContent,
                          img:re_imgs,
                        },
                        header: {
                          'content-type': 'application/json',
                          'token':wx.getStorageSync('token_key')
                        }, success: function (json) {
                          if(json.data.done==false){
                              wx.showModal({
                                       title: '提示',
                                       content: json.data.message,
                                       success: function(res) {
                                        if (res.confirm) {
                                     
                                        }
                                       }
                                      })
                          }else{
                            wx.showToast({
                                title: '提交成功',
                                icon: 'succes',
                                duration: 3000,
                                mask:true,
                                success:function(res){
                                    wx.navigateBack();
                                }
                            })


                          }
                        
                        }
                      })
      }else{
        console.log('已经提交过申请了');
           wx.showModal({
                                       title: '提示',
                                       content: '系统繁忙，请稍后再试',
                                       success: function(res) {
                                        if (res.confirm) {
                                        }
                                       }
                                      })
      }

    
  } 


})


